var songId;
const app = getApp();
const http = require("../../http/http");
const util = require("../../utils/util");
var _that
Component({
  properties: {
    item: { // 属性名
      type: Object,
      value: {}
    },
    //index
    index: Number,
    songlist: Object,
  },
  data: {
    ifStop: true,
  },

  lifetimes: {
    created() {

    },

    attached() {

      //刷新时检测播放暂停状态改变
      if (this.properties.item.id == getApp().globalData.correctSongId) {
        this.setData({
          ifStop: getApp().globalData.ifStop
        }),
        console.log("更新song-list组件显示状态为："+getApp().globalData.ifStop)
      }
    }
  },


  methods: {

    //点击歌曲播放按钮触发
    chooseSong(e) {


      if (this.properties.item.id != getApp().globalData.correctSongId) {
        console.log("点击的和正在播放的不是同一首")

        //播放新歌曲
        //获取歌曲ID index 所在songlist
        var id = e.currentTarget.dataset.id;
        var index = e.currentTarget.dataset.index;
        var songlist = e.currentTarget.dataset.songlist;
        console.log("index:" + index);

        //把index和所在的歌单保存至全局变量
        app.globalData.index = index;
        app.globalData.correctSongList = songlist;
        console.log(app.globalData.correctSongList);

        this.getSongInfoAndPlay(id);
      } else {


        console.log("点击的和正在播放的是同一首")
        //控制播放和暂停
        //还未播放
        if (this.data.ifStop == true) {
          //播放
          util.songPlayByUrl();
          //同步播放状态
          this.setData({
            ifStop: false,
          })
        } else {
          //暂停当前歌曲
          util.songPause()
          //同步播放状态
          this.setData({
            ifStop: true,
          })
        }
      }




    },
    // 点击歌 获取所有信息
    getSongInfoAndPlay(id) {
      http.getSongInformation(id).then(res => {
        //当前播放歌曲信息:
        app.globalData.correctSongId = id; //ID
        app.globalData.correctSongName = res.name; //歌曲名
        app.globalData.correctSongArtists = res.artists, //歌手s
          app.globalData.correctSongArtist = res.artistName, //歌手
          app.globalData.correctSongAlbum = res.albumName, //专辑
          app.globalData.songImg = res.coverImgUrl, //歌曲图标
          app.globalData.songPlayUrl = res.playUrl, // 歌曲播放URL
          app.globalData.songDuration=res.duration //歌曲时长
        //获取评论
        http.getSongCommonts(id).then(res => {
          getApp().globalData.songComments = res.records
        });

        console.log("赋值给全局变量完毕，开始播放")
        util.songPlayByUrl();

        //同步播放状态
        this.setData({
          ifStop: getApp().globalData.ifStop,
        })

        //刷新所在页面
        var page = getCurrentPages().pop();
        // console.log(page)
        if (page != undefined && page != null) {
          page.onShow();
        };

      });
    },
    
  }



})